import { request } from '@/utils/request';

// 用户接口类型定义
export interface User {
  id?: number;
  username: string;
  realName: string;
  email: string;
  password?: string;
  status: number;
  createTime?: string;
  updateTime?: string;
}

export interface UserListParams {
  page?: number;
  pageSize?: number;
  keyword?: string;
  username?: string;
  realName?: string;
  email?: string;
  status?: string;
  createTime?: string[];
}

export interface UserListResponse {
  list: User[];
  total: number;
  page: number;
  pageSize: number;
}

// 获取用户列表
export const getUserList = (params: UserListParams): Promise<UserListResponse> => {
  return request.get({
    url: '/admin/user',
    params: {
      ...params,
      tenantId: 1, // 默认租户ID，需要根据实际业务调整
    },
  });
};

// 根据ID获取用户详情
export const getUserById = (id: number): Promise<User> => {
  return request.get({
    url: `/admin/user/${id}`,
  });
};

// 创建用户
export const createUser = (data: User): Promise<void> => {
  return request.post({
    url: '/admin/user',
    data,
  });
};

// 更新用户
export const updateUser = (id: number, data: Partial<User>): Promise<void> => {
  return request.put({
    url: `/admin/user/${id}`,
    data,
  });
};

// 删除用户
export const deleteUser = (id: number): Promise<void> => {
  return request.delete({
    url: `/admin/user/${id}`,
  });
};

// 批量删除用户
export const batchDeleteUsers = (ids: number[]): Promise<void> => {
  return request.delete({
    url: '/admin/user/batch',
    data: { ids },
  });
};

// 导出用户列表
export const exportUsers = (params: UserListParams): Promise<Blob> => {
  return request.get({
    url: '/admin/user/export',
    params,
    responseType: 'blob',
  });
};
